import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/measures_task_record_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///历史执行情况
class JobHistoricalExecutionStatusController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  String startTime = '';
  String endTime = '';
  GetDictAllEntity? dictEntity1;

  List<GetDictAllEntity> currencyState = [];

  /// 落实情况
  String implementationStatus = '请选择';
  String implementationStatusId = '';

  @override
  void onInit() {
    super.onInit();
    // 当前完成情况(0:逾期，1已完成)
    currencyState.clear();
    final GetDictAllEntity getDictAllEntity = GetDictAllEntity();
    getDictAllEntity.id = '0';
    getDictAllEntity.dictionaryLabel = '逾期';
    currencyState.add(getDictAllEntity);

    final GetDictAllEntity getDictAllEntity2 = GetDictAllEntity();
    getDictAllEntity2.id = '1';
    getDictAllEntity2.dictionaryLabel = '已完成';
    currencyState.add(getDictAllEntity2);
  }

  /// 当前完成情况(0:逾期，1已完成)
  void selectCurrencyDangerLevelType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '状态',
      dataList: currencyState,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
        implementationStatusId = value[0].getId();
        implementationStatus = value[0].getShowName().isEmpty ? '请选择' : value[0].getShowName();
        update();
      },
    );
  }

  Future<List<MeasuresTaskRecordListEntity>> measuresTaskRecordList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<MeasuresTaskRecordListEntity>>(
      Interface.measuresTaskRecordList,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'startTime': startTime, //	区间开始时间
        'endTime': endTime, //	区间结束时间
        'checkTaskId': Get.arguments, //	履责工作内容id
        'controllable': implementationStatusId, //	当前完成情况(0:逾期，1已完成)
      },
    ).execute();
    List<MeasuresTaskRecordListEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  ///  重置
  void reset() {
    implementationStatusId = '';
    implementationStatus = '请选择';
    riskRightScaffoldKey.currentState?.closeEndDrawer();
    globalKey.currentState!.onRefresh();
    update();
  }

  ///  确定
  void confirm() {
    Get.back();
    globalKey.currentState!.onRefresh();
    update();
  }

  ///  搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  ///  筛选按钮
  void onFilterButtonPressed() {
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  ///  详情
  void clickDetail(MeasuresTaskRecordListEntity item) {
    //	历史履职情况列表中的id
    Get.toNamed(AppRoutes.jobLatestExecutionDetailsPage, arguments: {'id': item.id});
  }
}
